package fr.gest4team.app.pilotage.metier;

import java.util.Set;

import fr.gest4team.app.pilotage.dto.Activite;
import fr.gest4team.app.pilotage.dto.Planning;
import fr.gest4team.app.pilotage.dto.Tache;
import fr.gest4team.app.pilotage.dto.Version;

/**
 * 
 * @author brug
 * 
 */
public class VersionOm extends Version {

	/**
	 * universal id de la classe
	 */
	// static final long serialVersionUID = 0L;

	/**
	 * @param p
	 *            :
	 */
	@SuppressWarnings("unchecked")
	public void addTache(final TacheOm p) {
		getTaches().add(p);
	}

	/**
	 * @return Set<Tache>
	 */
	@Override
	@SuppressWarnings("unchecked")
	public Set getTaches() {
		return super.getTaches();
	}

	/**
	 * @param tachesParam
	 *            :
	 */
	@Override
	@SuppressWarnings("unchecked")
	public void setTaches(final Set tachesParam) {
		super.setTaches(tachesParam);
	}

	public float getChargeDev() {
		final Set<Tache> taches = getTaches();
		float charge = 0;
		for (final Tache tache : taches) {
			charge += tache.getChargeDev();
		}
		return charge;
	}

	public float getConsommationDev() {
		final Set<Tache> taches = getTaches();
		float conso = 0;
		for (final Tache tache : taches) {
			final Set<Planning> plannings = tache.getPlannings();
			for (final Planning planning : plannings) {
				final Set<Activite> activites = planning.getActivites();
				for (final Activite activite : activites) {
					conso += activite.getTemps();
				}
			}
		}
		return conso;
	}

	public float getEcartDev() {
		return getChargeDev() - getConsommationDev() - getRafDev();
	}

	public float getAvancementDev() {
		final float chargeDev = getChargeDev();
		final float rafDev = getRafDev();
		return (chargeDev != 0) ? ((chargeDev - rafDev) / chargeDev) * 100 : 0;
	}

	public float getRafDev() {
		final Set<Tache> taches = getTaches();
		float raf = 0;
		for (final Tache tache : taches) {
			final Set<Planning> plannings = tache.getPlannings();
			for (final Planning planning : plannings) {
				final float rafTache = tache.getChargeDev() - planning.getCharge() + planning.getRaf();
				raf += rafTache;
			}
		}
		return raf;
	}
}
